<?php

namespace app\api\controller;

use app\api\model\CommissionLog;
use app\api\model\WxappPage;
use app\api\model\Goods as GoodsModel;
use app\common\library\helper;
use app\common\model\ReadLog;
use think\Log;
use think\Request;

/**
 * 首页控制器
 * Class Index
 * @package app\api\controller
 */
class Index extends Controller
{
    private $trans = [1 => '一', 2 => '二', 3 => '三', 4 => '四', 5 => '五', 6 => '六', 7 => '七', 8 => '八', 9 => '九', 10 => '十', 11 => '十一', 12 => '十二'];

    /**
     * 首页diy数据
     * @return array
     * @throws \think\exception\DbException
     */
    public function page()
    {
        // 页面元素
        $wxappPage = WxappPage::detail();
        $items = $wxappPage['page_data']['array']['items'];
        // 新品推荐
        $model = new GoodsModel;
        $newest = $model->getNewList();
        // 猜您喜欢
        $best = $model->getBestList();
        return $this->renderSuccess(compact('items', 'newest', 'best'));
    }

    public function searches()
    {
        $currentMonth = intval(date('m'));
        $months = [];
        for ($i = 0; $i < 12; $i++) {
            $months[] = $this->trans[$i + 1] . '月';
            if ($currentMonth == $i + 1) {
                $monthIndex = $i;
            }
        }
        $years = [];
        $currentYear = date('Y');
        for ($y = 0; $y < 10; $y++) {
            $years[] = ($currentYear - $y) . '年';
            if ($currentYear == $currentYear - $y) {
                $yearIndex = $y;
            }
        }
        $seasons = [
            '一季度',
            '二季度',
            '三季度',
            '四季度',
        ];
        if ($currentMonth < 4) $seasonIndex = 0;
        elseif (3 < $currentMonth && $currentMonth < 7) $seasonIndex = 1;
        elseif (6 < $currentMonth && $currentMonth < 10) $seasonIndex = 2;
        elseif (9 < $currentMonth && $currentMonth < 13) $seasonIndex = 3;
        $searchType = 'month';
        return $this->renderSuccess(compact('years', 'months', 'seasons', 'monthIndex', 'yearIndex', 'seasonIndex', 'searchType'));
    }

    /**
     * @return array
     */
    public function agent_calculate(Request $request)
    {
        $input = input();
        $model = new CommissionLog();
        $user = $this->getUser();
        $res = $model->getAgentCalculatePage($user, $input, $request);
        return $this->renderSuccess($res);
    }

    public function agent_sales(Request $request)
    {
        $input = input();
        $model = new CommissionLog();
        $user = \app\api\model\User::get($input['userId']);
        $res = $model->getAgentSales($user, $input, $request);
        return $this->renderSuccess($res);
    }

    public function agent_info($level)
    {
        $agents = helper::getAgents();
        $agentInfo = $agents[$level];
        return $this->renderSuccess(compact('agentInfo'));
    }

    public function log()
    {
        $options = input('options');
        $userId = input('user_id');
        $model = new ReadLog();
        $data = [
            'user_id' => $userId,
            'create_time' => time(),
            'page' => input('page'),
            'options' => htmlspecialchars_decode($options) ,
            'scene' => input('scene')
        ];
        $model->save($data);
        return $this->renderSuccess($model->read_log_id);
    }
}
